package com.example.wechathook.hook;

import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

/**
 * 
 * hook进入某个界面的Oncreate
 * 
 * @author chencq
 *
 */
public class ActivityHook extends BaseHook {

	public void hookOncreate(XC_LoadPackage.LoadPackageParam lpp, String activityClazz) {
		findAndHookMethod(activityClazz, lpp.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
			@Override
			protected void afterHookedMethod(MethodHookParam param) throws Throwable {
				super.afterHookedMethod(param);
				Activity activity = (Activity) param.thisObject;
				Toast.makeText(activity, "hook oncreate", Toast.LENGTH_SHORT).show();
			}
		});
	}

	public void hookOnResume(XC_LoadPackage.LoadPackageParam lpp, String activityClazz) {
		findAndHookMethod(activityClazz, lpp.classLoader, "onResume", new XC_MethodHook() {
			@Override
			protected void afterHookedMethod(MethodHookParam param) throws Throwable {
				super.afterHookedMethod(param);

				final Activity activity = (Activity) param.thisObject;
				final ViewGroup contentView = (ViewGroup) activity.getWindow().getDecorView();
				final Handler handler = new Handler(Looper.getMainLooper());

				TextView textView = findTextView(contentView, "我");
				float x = 0;
				float y = 0;
				if (textView != null) {
					int[] location = new int[2];
					textView.getLocationOnScreen(location);
					x = textView.getWidth() / 2 + location[0];
					y = textView.getHeight() / 2 + location[1];
					simulateClick(x, y);
				}

				Toast.makeText(activity, "hook onResume,x=" + x + ",y=" + y, Toast.LENGTH_SHORT).show();
			}
		});
	}

	@Override
	public void hook(LoadPackageParam lpp) {

	}

}
